﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl2 : MonoBehaviour
{

    Animator animator;

    public GameObject effectToPlay;

    private void Start()
    {
        animator = this.GetComponent<Animator>();
    }

    private void Update()
    {
        if (Const.isgameover)
        {
            animator.SetTrigger("isidle");
        }
        else
        {
            //animator.SetBool("ismove", true);
            animator.SetTrigger("ismove");
        }
    }

    public void PlayerDie()
    {
        animator.SetTrigger("isdie");
    }

    public void PlayerAttack()
    {
        animator.SetTrigger("isattack");
        StartCoroutine(PlayEffect());
    }


    IEnumerator PlayEffect()
    {
        yield return new WaitForSeconds(0.4f);

        // Distance from your player    
        float distance = 5;

        // Transforms a forward position relative to your player into the world space    
        Vector3 effectPos = this.transform.TransformPoint(this.transform.forward * distance);
        //effectPos.y = effectPos.y + 1.2f;
        //effectPos.x = effectPos.x - 1.5f;
        effectPos.y += 0.2f;
        // Instantate the particle effect in front of the object  
        //Transform effect = Instantiate(effectToPlay, effectPos, Quaternion.identity) as Transform;
        Transform effect = Instantiate(effectToPlay, effectPos, Quaternion.identity).transform;
        // Makes the particle effect "One Shot" or 
        // automatically destroys the particle after playing by adding "EFfectSelfDestruct" script.
        effect.gameObject.AddComponent<EffectSelfDestruct>();
    }
}
